package pandabay.web;

import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import pandabay.DAO.UserDAO;
import pandabay.service.NewUser;
import pandabay.service.ProductManager;

public class NewUserFormController extends SimpleFormController {

    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());

    private ProductManager productManager;
    private UserDAO userDAO;

    public ModelAndView onSubmit(Object command)
            throws ServletException {

        String increase = ((NewUser) command).getForename();
//        NewUser nu = (NewUser) command;
//        try {
//			userDAO.saveUser(nu);
//		} catch (Exception e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
        logger.info("Increasing prices by " + increase + "%.");

        //productManager.increasePrice(increase);

        logger.info("returning from NewUserForm view to " + getSuccessView());

        return new ModelAndView(new RedirectView(getSuccessView()));
    }

    protected Object formBackingObject(HttpServletRequest request) throws ServletException {
        NewUser newUser = new NewUser();
        //newProduct.setPercentage(20);
        return newUser;
    }

    public void setProductManager(ProductManager productManager) {
        this.productManager = productManager;
    }

    public ProductManager getProductManager() {
        return productManager;
    }

}